﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FAS.App_Code;
using FAS.Objetos;

namespace FasGUI
{
    public partial class FormObjetivosExamen : Form
    {
        private Form inicio;
        private List<String> IdViejos = new List<String>();
        public FormObjetivosExamen(Form inicio)
        {
            this.inicio = inicio;
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
            fillAll();
            comboBox2.SelectedIndex = 0;
            comboBox3.SelectedIndex = 0;
        }


        public void fillAll()
        {
            ConexionMySQL conexion = new ConexionMySQL();
            DataSet data = conexion.GetQuery("SELECT * FROM materia");  
            comboBox2.DisplayMember = "ClaveMateria";
            comboBox2.ValueMember = "IdMateria";
            comboBox2.DataSource = data.Tables[0];
            selectedMateria();
        }

        private void selectedMateria()
        {
            int id = int.Parse(comboBox2.SelectedValue.ToString());
            lblMateria.Text = Materia.GetNombreMateria(id);
        }
        private static bool tryParse(string num)
        {
            try
            {
                int.Parse(num); 
                return true;
            }
            catch (Exception e) { 
                return false; 
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if ( comboBox2.Text != "" && int.Parse(this.numericUpDown1.Value.ToString()) > 0)
            {
                bool b = false;
                Dictionary<Objetivo, int> dic = new Dictionary<Objetivo, int>();
                foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {
                    DataGridViewCheckBoxCell check = ((DataGridViewCheckBoxCell)row.Cells[0]);
                    DataGridViewTextBoxCell objetivo = ((DataGridViewTextBoxCell)row.Cells[1]);
                    DataGridViewTextBoxCell total = ((DataGridViewTextBoxCell)row.Cells[2]);
                    DataGridViewTextBoxCell cantidad = ((DataGridViewTextBoxCell)row.Cells[3]);
                    int cant=0;
                    if (tryParse(cantidad.Value.ToString()))
                    {
                        cant = int.Parse(cantidad.Value.ToString());
                        if (cant <= int.Parse(total.Value.ToString()))
                        {
                            b = true;
                            if (cant > 0)
                            {
                                Objetivo obj = new Objetivo();
                                obj.IdObjetivo = objetivo.Tag.ToString();
                                obj.IdMateria = int.Parse(comboBox2.SelectedValue.ToString());
                                dic.Add(obj, cant);
                            }
                        }
                        else
                        {
                            b = false;
                            MessageBox.Show("El número de problemas seleccionados no debe ser mayor que el total del objetivo ");
                            break;
                        }
                    }
                    else
                    {
                        b = false;
                        MessageBox.Show("Valor incorrecto, introducir un valor numérico");
                        break;
                    }
                }
                if (b)
                {
                    Parcial parcial = new Parcial((this.comboBox1.SelectedIndex + 1), this.lblMateria.Text, comboBox3.Text, null);
                    parcial.GenerarExamenes(int.Parse(this.numericUpDown1.Value.ToString()), dic, comboBox3.Text+DateTime.Now.Year, int.Parse(comboBox2.SelectedValue.ToString()));

                    this.Dispose();
                    inicio.Show();
                }
            }
            else
            {
                if (comboBox2.Text == "")
                    errorProvider1.SetError(comboBox2, "Introducir nombre de la Máteria");
                if (int.Parse(this.numericUpDown1.Value.ToString()) <= 0)
                    errorProvider1.SetError(numericUpDown1, "El número de examenes debe ser mayor a 0");
            }

        }

        private void FormObjetivosExamen_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            inicio.Show();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.Dispose();
            inicio.Show();
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.Items.Count > 0)
            {
                selectedMateria();
                //llenar objetivos
                this.IdViejos.Clear();
                int idM = int.Parse(comboBox2.SelectedValue.ToString());
                this.dataGridView1.Rows.Clear();
                DataTable data = new ConexionMySQL().GetQuery("SELECT * FROM `Objetivo` where IdMateria = '" + idM + "'").Tables[0];
                foreach (DataRow rowD in data.Rows)
                {
                    this.IdViejos.Add(rowD["IdObjetivo"].ToString());
                    DataGridViewRow row = new DataGridViewRow();
                    DataGridViewCheckBoxCell cell = new DataGridViewCheckBoxCell();
                    cell.Value = false;
                    DataGridViewTextBoxCell celdaNombre = new DataGridViewTextBoxCell();
                    celdaNombre.Value = rowD["Nombre"].ToString();
                    celdaNombre.Tag = rowD["IdObjetivo"].ToString();
                    DataGridViewTextBoxCell celdaPCount = new DataGridViewTextBoxCell();
                    DataTable data2 = new ConexionMySQL().GetQuery("SELECT COUNT(IdProblema) as Cuenta FROM `Problema` where IdMateria = '" + idM + "' AND IdObjetivo = '" + rowD["IdObjetivo"].ToString() + "'").Tables[0];
                    celdaPCount.Value = data2.Rows[0]["Cuenta"].ToString();
                    DataGridViewTextBoxCell celdaNumero = new DataGridViewTextBoxCell();
                    celdaNumero.Value = "0";
                    row.Cells.Add(cell);
                    row.Cells.Add(celdaNombre);
                    row.Cells.Add(celdaPCount);
                    row.Cells.Add(celdaNumero);
                    this.dataGridView1.Rows.Add(row);
                }
                this.dataGridView1.Columns[1].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
                this.dataGridView1.Columns[1].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            }
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int total = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
                total += int.Parse(row.Cells[3].Value.ToString());
            lblTotal.Text = total.ToString();
        }

        private void evento(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView1_CellContentClick(null,null);
        }

        private void button3_Click(object sender, EventArgs e)
        {

            System.Diagnostics.Process.Start("hojaderespuestas.pdf");
        }

    }
}
